import 'dart:math';

import 'package:flutter/foundation.dart' show ChangeNotifier;
import 'package:flutter/foundation.dart';
import 'package:message_tools/model/sms_task.dart';
import 'package:message_tools/model/sms_template.dart';
import 'package:message_tools/service/db_repository.dart';
import 'package:message_tools/utils/date_utils.dart';

class SmsTaskModel with ChangeNotifier {
  List<SmsTask> _list = [];
  SmsTask _smsTask = new SmsTask();
  String _preMessage = "";

  String _selectFilePath = "";
  List _previewSelectContacts = [];

  List<SmsTask> get list => _list;

  SmsTask get smsTask => _smsTask;

  String get preMessage => _preMessage;

  String get selectFilePath => _selectFilePath;

  void setEditSmsTask(SmsTask smsTask) {
    _smsTask = smsTask;
    computePreMessage();
    notifyListeners();
  }

  loadData({int pageNum}) {
    DbRepository.fetchSmsTaskList().then((data) {
      _list = data;
      notifyListeners();
    });
  }

  void save(SmsTask smsTask) {
    debugPrint("smsTask = ${smsTask.id} \n");
    if (smsTask.id == null) {
      debugPrint("add smsTask \n");
      add(smsTask);
    } else {
      debugPrint("update smsTask \n");
      update(smsTask);
    }
  }

  void add(SmsTask smsTask) {
    DbRepository.insertSmsTask(smsTask);
    _list.insert(0, smsTask);
    notifyListeners();
  }

  void update(SmsTask smsTask) {
    DbRepository.updateSmsTask(smsTask);
    _list.forEach((item) {
      if (smsTask.id == item.id) {
        item = smsTask;
      }
    });

    notifyListeners();
  }

  void setPreviewSelectContacts(List contacts) {
    _previewSelectContacts = contacts;
    computePreMessage();
  }

  void setSelectSmsTemplate(SmsTemplate selectSmsTemplate) {
    _smsTask.smsTemplateTitle = selectSmsTemplate.title;
    _smsTask.smsTemplateContent = selectSmsTemplate.content;
    computePreMessage();
  }

  void setSelectFilePath(String filePath) {
    _selectFilePath = filePath;
    _smsTask.contactFilePath = filePath;

    notifyListeners();
  }

  void computePreMessage() {
    _preMessage = computedPreMessage(_smsTask, _previewSelectContacts);
    debugPrint("_preMessage = $_preMessage \n");

    notifyListeners();
  }

  static String computedPreMessage(
      SmsTask smsTask, List previewSelectContacts) {
    var preMessage = smsTask.smsTemplateContent
        .replaceAll("{|t|}", DateUtil.formatNowTime("HH:mm"));
    preMessage =
        preMessage.replaceAll("{|d|}", DateUtil.formatNowTime("yyyy年MM月dd日"));
    while (true) {
      if (preMessage.indexOf("{|n|}") < 0) {
        break;
      }

      int random = Random().nextInt(9);
      preMessage = preMessage.replaceFirst("{|n|}", random.toString());
    }

    if (previewSelectContacts == null) {
      return preMessage;
    }

    previewSelectContacts.forEach((item) {
      if (item.length > 1) {
        preMessage = preMessage.replaceAll("{|e1|}", item[1]);
      }
      if (item.length > 2) {
        preMessage = preMessage.replaceAll("{|e2|}", item[2]);
      }
      if (item.length > 3) {
        preMessage = preMessage.replaceAll("{|e3|}", item[3]);
      }
    });

    return preMessage;
  }

  static String computedPreMessageX(SmsTask smsTask, List contactCells) {
    var preMessage = smsTask.smsTemplateContent
        .replaceAll("{|t|}", DateUtil.formatNowTime("HH:mm"));
    preMessage =
        preMessage.replaceAll("{|d|}", DateUtil.formatNowTime("yyyy年MM月dd日"));
    while (true) {
      if (preMessage.indexOf("{|n|}") < 0) {
        break;
      }

      int random = Random().nextInt(9);
      preMessage = preMessage.replaceFirst("{|n|}", random.toString());
    }

    if (contactCells == null) {
      return preMessage;
    }

    if (contactCells.length > 1) {
      preMessage = preMessage.replaceAll("{|e1|}", contactCells[1]);
    }
    if (contactCells.length > 2) {
      preMessage = preMessage.replaceAll("{|e2|}", contactCells[2]);
    }
    if (contactCells.length > 3) {
      preMessage = preMessage.replaceAll("{|e3|}", contactCells[3]);
    }

    return preMessage;
  }
}
